package com.controller.qsl;

import com.dao.qsl.AddBxDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@Controller
public class Tp {

      @Autowired
      private AddBxDao a;
      @RequestMapping(value="/QslPage/upload")

      @ResponseBody
      public Boolean ufile(MultipartFile tp, HttpServletRequest request, HttpServletResponse response, HttpSession session , String lxdh , String bxlr){
//

           System.out.println(bxlr);
            System.out.println(lxdh);

            try {
                  bxlr=new String(bxlr.getBytes("ISO-8859-1"),"UTF-8");
                  System.out.println(bxlr);
            } catch (UnsupportedEncodingException e) {
                  throw new RuntimeException(e);
            }

            //获取文件名 : file.getOriginalFilename();
            String uploadFileName = tp.getOriginalFilename();


            System.out.println("文件名："+uploadFileName);

            //上传路径保存设置
            String path = request.getSession().getServletContext().getRealPath("/img");
            File realPath = new File(path);
            if (!realPath.exists()){
                  realPath.mkdir();
            }
            //上传文件地址
            System.out.println("上传文件保存地址："+realPath);

            //通过CommonsMultipartFile的方法直接写文件（注意这个时候）

            Boolean jg=a.add(request,bxlr,lxdh,uploadFileName);

            System.out.println("jg:"+jg);
            try {
                  tp.transferTo(new File(realPath +"/"+ tp.getOriginalFilename()));
            }catch (Exception e){
                  e.printStackTrace();
            }
            return  jg ;
          }

}
